/*
 * @Description: 表格管理类器
 * @Author: zhp
 * @LastEditors  : xutao
 * @LastEditTime : 2020-01-16 21:00:09
 */

import TeMap from '../core/TeMap';
import TableLoader from './TableLoader';

//所有的表格名字
export let TableName = {
  config: 'config',
};

export default class TableMgr {
  private static _inst: TableMgr;
  public static get inst(): TableMgr {
    if (!this._inst) {
      this._inst = new TableMgr();
    }
    return this._inst;
  }
  // 禁止外部构造对象
  private constructor() {}

  private tableLoader: TableLoader;
  async loadTableRes(): Promise<any> {
    //<Map<string, TableConfig>> {
    if (this.tableLoader) return;
    this.tableLoader = new TableLoader(TableName, this._onTableLoaded.bind(this));
    return this.tableLoader.loadTableRes();
  }

  /**
   * @param name 通过表格名字，获取表格内容
   */
  private getTableByName(name: string): any {
    return this.tableLoader.tableMap[name].data;
  }

  private _onTableLoaded(): void {
    this._initGameConfig();
  }

  /**
   *
   * @param name 表格名字
   * @param key 键值
   * @param defaultValue 取值不到时的默认值
   */
  public getTableValue(name: string, key: any, defaultValue = null): any {
    let t = this.getTableByName(name);
    if (t) {
      let v = t[String(key)];
      return v ? v : defaultValue;
    }
    return defaultValue;
  }

  /**
   * 初始化游戏配置数据
   */
  private _gameConfig: any = {};
  private _initGameConfig(): void {
    let data = this.getTableByName(TableName.gameConfig);
    Object.keys(data).forEach((key) => {
      let value: res_gameConfig = data[key];
      switch (value.type) {
        case 'number':
          value.value = <any>parseInt(value.value);
          break;
        case 'number[]':
          value.value = <any>value.value.split(',');
          let n_arr: number[] = [];
          for (let i: number = 0; i < value.value.length; i++) {
            n_arr.push(parseInt(value.value[i]));
          }
          value.value = <any>n_arr;
          break;
        case 'string[]':
          value.value = <any>value.value.split(',');
          break;
      }
      this._gameConfig[value.key] = value.value;
    });
  }
  /**
   * 取全局变量配置
   */
  public getConfigKey(key: string, defaultValue?: any): any {
    if (this._gameConfig && this._gameConfig[key]) {
      return this._gameConfig[key];
    } else if (this._gameConfig && defaultValue) {
      return defaultValue;
    } else {
      return null;
    }
    //return this.getTableValue(TableName.config, key, defaultValue)
  }

  /**
   * 初始化表现
   */
  /**
   * 初始化棋子数据
   */
  // private _dragonConfigMap: TeMap<res_dragonNode> = new TeMap<res_dragonNode>();
  // private _initDragonNode(): void {
  //     let data = this.getTableByName(TableName.dragonNode);
  //     Object.keys(data).forEach(key => {
  //         let value: res_dragonNode = data[key];
  //         this._dragonConfigMap.set(value.id, value);
  //     });
  // }

  // public getDragonConfigById(id:number):res_hero{
  //     return this._dragonConfigMap.get(id)
  // }

  // /**
  //  * 初始化 特效
  //  */
  // private _effectMap: TeMap<res_dragonEffect> = new TeMap<res_dragonEffect>();
  // private _initEffect(): void {
  //     let data = this.getTableByName(TableName.dragonEffect);
  //     Object.keys(data).forEach(key => {
  //         let value: res_dragonEffect = data[key];
  //         this._dragonConfigMap.set(value.name, value);
  //     });
  // }

  // public getEffectByName(name:string):res_hero{
  //     return this._dragonConfigMap.get(name)
  // }
}
